package com.zhongwang.mms.module.allocation.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.allocation.dao.AllocationOrderDao;
import com.zhongwang.mms.module.allocation.model.AllocationOrderModel;
import com.zhongwang.mms.module.allocation.service.AllocationInWarService;
import com.zhongwang.mms.module.allocation.service.AllocationOrderService;
import com.zhongwang.mms.module.half.service.HBasicService;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @description:调拨入库
 * @author: th
 * @time: 2020/8/21 9:08
 */
@Controller
@RequestMapping("/allocationInWar")
public class AllocationInWarController extends BaseController {

    //页面文件路径
    private String prefix = "allocation/allocationInWar";
    @Autowired
    private AllocationOrderService allocationOrderService;
    @Autowired
    private AllocationOrderDao allocationOrderDao;
    @Autowired
    private AllocationInWarService allocationInWarService;
    @Autowired
    private HBasicService hBasicService;
    //加载页面
    @GetMapping("/page")
    @RequiresPermissions({"allocation:in:view"})
    public String create(Model model) {

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("userName", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return prefix + "/page";    }

    /**
     *  调拨入库页面数据
     * @param query
     * @return
     */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"allocation:in:view"})
    public JsonResult list(AllocationOrderModel query){

        startPage();
        query.setOdrStatus(3);
        query.setOdrOutStatus(2);
        List<AllocationOrderModel> list = allocationOrderService.selectAllByQuery(query);
        return jsonResult(list);
    }

    /**
     * 选择入库单
     * @param model
     * @return
     */
    @GetMapping("/allocationInOrderDetail/{odrId}")
    @RequiresPermissions({"allocation:in:view"})
    public String allocationInOrderDetail(Model model,@PathVariable("odrId") Integer odrId) {

        AllocationOrderModel allocationOrder = allocationOrderDao.selectAllByPrimaryKey(odrId);

        // 获取登入用户实体
        User user = ShiroUtils.getUser();
        model.addAttribute("odrMakePerson", user.getName());
        model.addAttribute("odrDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        model.addAttribute("odrId",odrId);
        model.addAttribute("dept", hBasicService.getDept());

        if(allocationOrder.getOdrType() == 1){
            return prefix + "/purInPage";
        }else if(allocationOrder.getOdrType() == 2){
            return prefix + "/matInPage";
        }else {
            return prefix + "/halInPage";
        }
    }

    /**
     * 修改调拨入库
     * @param param
     * @return
     */
    @ResponseBody
    @PostMapping("/updateAllocationInWarNum")
    @RequiresPermissions({"allocation:in:edit"})
    public JsonResult updateAllocationInWarNum(AllocationOrderModel param){

        return allocationInWarService.updateAllocationInWarNum(param);
    }

    /**
     * 调拨入库保存
     * @param param
     * @return
     */
    @ResponseBody
    @PostMapping("/saveAllocationInWar")
    @RequiresPermissions({"allocation:in:edit"})
    public JsonResult saveAllocationInWar(AllocationOrderModel param){
        return allocationInWarService.saveAllocationInWar(param);
    }
}
